home *** CD-ROM | disk | FTP | other *** search
/ Collection of Internet / Collection of Internet.iso / msdos / lynx / source / www / library / implemen / hthistor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-25  |  4.0 KB  |  162 lines

  1. #include"capalloc.h"
  2. #include"capstdio.h"
  3. #include "HTHistory.h"
  4.  
  5. #include "tcp.h"        /* for standard io */
  6.  
  7. static HTList * history;    /* List of visited anchors */
  8.  
  9.  
  10. /*                Navigation
  11. **                ==========
  12. */
  13.  
  14. /*        Record the jump to an anchor
  15. **        ----------------------------
  16. */
  17.  
  18. void HTHistory_record
  19.   ARGS1 (HTAnchor *,destination)
  20. {
  21.   if (destination) {
  22.     if (! history)
  23.       history = HTList_new();
  24.     HTList_addObject (history, destination);
  25.   }
  26. }
  27.  
  28. /*        Go back in history (find the last visited node)
  29. **        ------------------
  30. */
  31.  
  32. HTAnchor * HTHistory_backtrack
  33.   NOARGS  /* FIXME: Should we add a `sticky' option ? */
  34. {
  35.   if (HTHistory_canBacktrack())
  36.     HTList_removeLastObject (history);
  37.   return HTList_lastObject (history);  /* is Home if can't backtrack */
  38. }
  39.  
  40. BOOL HTHistory_canBacktrack
  41.   NOARGS
  42. {
  43.   return (HTList_objectAt (history, 1) != NULL);
  44. }
  45.  
  46. /*        Browse through references in the same parent node
  47. **        -------------------------------------------------
  48. **
  49. **    Take the n-th child's link after or before the one we took to get here.
  50. **    Positive offset means go towards most recently added children.
  51. */
  52.  
  53. HTAnchor * HTHistory_moveBy
  54.  ARGS1 (int,offset)
  55. {
  56.   HTAnchor * last = HTList_objectAt (history, 1);
  57.   if (! last)
  58.     return NULL;  /* No last visited node */
  59.   if (last != (HTAnchor *) last->parent) {  /* Was a child */
  60.     HTList * kids = last->parent->children;
  61.     int i = HTList_indexOf (kids, last); 
  62.     HTAnchor * nextOne = HTList_objectAt (kids, i - offset);
  63.     if (nextOne) {
  64.       HTAnchor * destination = HTAnchor_followMainLink (nextOne);
  65.       if (destination) {
  66.     HTList_removeLastObject (history);
  67.     HTList_removeLastObject (history);
  68.     HTList_addObject (history, nextOne);
  69.     HTList_addObject (history, destination);
  70.       }
  71.       return destination;
  72.     } else {
  73. #ifndef RELEASE
  74.       if (TRACE) fprintf(stderr,
  75.         "HTHistory_moveBy: offset by %+d goes out of list %p.\n",
  76.         offset, (void*)kids);
  77. #endif /* RELEASE */
  78.       return NULL;
  79.     }
  80.   } else {  /* Was a parent */
  81.     return NULL;  /* FIXME we could possibly follow the next link... */
  82.   }
  83. }
  84.  
  85. BOOL HTHistory_canMoveBy
  86.  ARGS1 (int,offset)
  87. {
  88.   HTAnchor * last = HTList_objectAt (history, 1);
  89.   if (! last)
  90.     return NO;  /* No last visited node */
  91.   if (last != (HTAnchor *) last->parent) {  /* Was a child */
  92.     HTList * kids = last->parent->children;
  93.     int i = HTList_indexOf (kids, last); 
  94.     return (HTList_objectAt (kids, i - offset) != NULL);
  95.   } else {  /* Was a parent */
  96.     return NO;  /* FIXME we could possibly follow the next link... */
  97.   }
  98. }
  99.  
  100.  
  101. /*                Retrieval
  102. **                =========
  103. */
  104.  
  105. /*        Read numbered visited anchor (1 is the oldest)
  106. **        ----------------------------
  107. */
  108.  
  109. HTAnchor * HTHistory_read
  110.   ARGS1 (int,number)
  111. {
  112.   return HTList_objectAt (history, HTList_count (history) - number);
  113. }
  114.  
  115.  
  116. /*        Recall numbered visited anchor (1 is the oldest)
  117. **        ------------------------------
  118. **    This reads the anchor and stores it again in the list, except if last.
  119. */
  120.  
  121. HTAnchor * HTHistory_recall
  122.   ARGS1 (int,number)
  123. {
  124.   HTAnchor * destination =
  125.     HTList_objectAt (history, HTList_count (history) - number);
  126.   if (destination && destination != HTList_lastObject (history))
  127.     HTList_addObject (history, destination);
  128.   return destination;
  129. }
  130.  
  131. /*        Number of Anchors stored
  132. **        ------------------------
  133. **
  134. **    This is needed in order to check the validity of certain commands
  135. **    for menus, etc.
  136. (not needed for now. Use canBacktrack, etc.)
  137. int HTHistory_count
  138.   NOARGS
  139. {
  140.   return HTList_count (history);
  141. }
  142. */
  143.  
  144. /*        Change last history entry
  145. **        -------------------------
  146. **
  147. **    Sometimes we load a node by one anchor but leave by a different
  148. **    one, and it is the one we left from which we want to remember.
  149. */
  150.  
  151. void HTHistory_leavingFrom
  152.   ARGS1 (HTAnchor *,anchor)
  153. {
  154.   if (HTList_removeLastObject (history))
  155.     HTList_addObject (history, anchor);
  156.   else    {
  157. #ifndef RELEASE
  158.     if (TRACE) fprintf(stderr, "HTHistory_leavingFrom: empty history !\n");
  159. #endif /* RELEASE */
  160.   }
  161. }
  162.